package com.example.earthquakeicg;

import java.util.ArrayList;
import android.app.Activity;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.Projection;
import com.amap.api.maps2d.AMap.InfoWindowAdapter;
import com.amap.api.maps2d.AMap.OnInfoWindowClickListener;
import com.amap.api.maps2d.AMap.OnMapClickListener;
import com.amap.api.maps2d.AMap.OnMapLoadedListener;
import com.amap.api.maps2d.AMap.OnMapLongClickListener;
import com.amap.api.maps2d.AMap.OnMarkerClickListener;
import com.amap.api.maps2d.model.BitmapDescriptor;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.LatLngBounds;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.example.earthquakeicg.Constants;
import com.example.earthquakeicg.ToastUtil;

import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.BounceInterpolator;
import android.view.animation.Interpolator;

public class MainActivity extends Activity   implements  OnMapClickListener,
OnMapLongClickListener,OnMarkerClickListener,OnClickListener, 
OnInfoWindowClickListener, OnMapLoadedListener, InfoWindowAdapter 
  {
  
    private MapView mapView;
    private AMap aMap;
   
    private TextView mTapTextView;
	private EditText mInputText; 
	private EditText mInputText2;
	private Button ybutton;
	private Button nbutton;
	
	private String mtitle="";
	private String mcontent="";
	private double mlatitude=0;
	private double mlongitude=0;		
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // R 需要引用包import com.amapv2.apis.R;
        setContentView(R.layout.activity_main);
        mapView = (MapView) findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);// 必须要写
        init();
        
    }

    private void init() {

        if (aMap == null) {
			aMap = mapView.getMap();
			setUpMap();
		}
        
        mTapTextView = (TextView) findViewById(R.id.tap_text);
		mInputText = (EditText) findViewById(R.id.inputtext);
		mInputText2 = (EditText) findViewById(R.id.EditText01);
		ybutton = (Button) findViewById(R.id.button1);
		nbutton = (Button) findViewById(R.id.button2);
		mInputText.setVisibility(View.INVISIBLE); 
		mInputText2.setVisibility(View.INVISIBLE); 
		ybutton.setVisibility(View.INVISIBLE); 
		nbutton.setVisibility(View.INVISIBLE); 
		
		ybutton.setOnClickListener(this);
		nbutton.setOnClickListener(this);
		//radioOption = (RadioGroup) findViewById(R.id.custom_info_window_options);
		
		
    }
 
	//@Override 
	private void setUpMap() {
	aMap.setOnMapClickListener(this);// 对amap添加单击地图事件监听器
	aMap.setOnMapLongClickListener(this);// 对amap添加长按地图事件监听器
	aMap.setOnMapLoadedListener(this);// 设置amap加载成功事件监听器
	aMap.setOnMarkerClickListener(this);// 设置点击marker事件监听器
	aMap.setOnInfoWindowClickListener(this);// 设置点击infoWindow事件监听器
	aMap.setInfoWindowAdapter(this);// 设置自定义InfoWindow样式
	addMarkersToMap();// 往地图上添加marker
	
	  }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }  
	/**
	 * 在地图上添加marker
	 */
	private void addMarkersToMap() {

		aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
				.position(Constants.Babaocun).title("八宝村沙子厂")
				.snippet("时间：2014-8-05；\n八宝村沙子厂房屋倒塌严重，没有伤亡，没帐篷，水，食物需要药品及食物。").draggable(true));
		
		aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
				.position(Constants.Laojiezi).title("龙头山镇光明村老街子")
				.snippet("时间：2014-8-04 ；\n道路全部堵塞，土房全部倒塌，山体开裂严重，可能出现山体滑坡！其中菜园子有200多人被困缺乏食物、饮用水、帐篷。4人遇难，救出两位小孩，其中一个已经没有生命迹象，另外还有两个大人被埋，村民正在救援。一个小时前有十几位官兵徒步进入，目前还没有到达村里！").draggable(true));
		
		aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
				.position(Constants.Tianshengqiaocun).title("天生桥村")
				.snippet("时间：2014-8-04 ；\n一半以上房屋倒塌，尚无救援力量到达。求救人余先生的妹妹被埋压。").draggable(true));
		
		aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
				.position(Constants.Shuijingwan).title("龙头山镇新民村水井湾社")
				.snippet("时间：2014-8-04 ；\n通往县城的路断了，有伤员出不去，现在有医生进入无救援队进入。刚下过暴雨，没有帐篷，缺少吃的，保暖衣物，也没有紧急避难场所。").draggable(true));
		
		aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
				.position(Constants.Zhichangxiang).title("纸厂乡安置点")
				.snippet("时间：2014-8-05 ；\n会泽县纸厂乡中学临时安置点，8台炊具车免费为受灾群众供应晚饭，四菜一汤，还有肉，1200余受灾群众就餐。").draggable(true));
		
		
		
		drawMarkers();// 添加10个带有系统默认icon的marker
		
		
	}

	public void drawMarkers() {
		// 动画效果
				ArrayList<BitmapDescriptor> giflist = new ArrayList<BitmapDescriptor>();
				giflist.add(BitmapDescriptorFactory
						.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
				giflist.add(BitmapDescriptorFactory
						.defaultMarker(BitmapDescriptorFactory.HUE_RED));
				giflist.add(BitmapDescriptorFactory
						.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
						
				Marker marker = aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
						.position(Constants.Zhenzhong).title("云南昭通6.5级地震（震源）").icons(giflist)
						.snippet("2014-8-03-16:30；震源深度12公里。。").draggable(true).period(3));
				
				marker.showInfoWindow();// 设置默认显示一个infowinfow
		
		
	}

	/**
	 * 对marker标注点点击响应事件
	 */
	@Override
	public boolean onMarkerClick(final Marker marker) {

		//jumpPoint(marker);	
		mTapTextView.setText("详细信息：" + marker.getTitle()+"\n"+"坐标："+marker.getPosition()+"\n"+marker.getSnippet());
		marker.showInfoWindow();		
		return false;
	}

	/**
	 * marker点击时跳动一下
	 */
	public void jumpPoint(final Marker marker) {
		final Handler handler = new Handler();
		final long start = SystemClock.uptimeMillis();
		Projection proj = aMap.getProjection();
		Point startPoint = proj.toScreenLocation(Constants.Zhenzhong);
		startPoint.offset(0, -100);
		final LatLng startLatLng = proj.fromScreenLocation(startPoint);
		final long duration = 1500;

		final Interpolator interpolator = new BounceInterpolator();
		handler.post(new Runnable() {
			@Override
			public void run() {
				long elapsed = SystemClock.uptimeMillis() - start;
				float t = interpolator.getInterpolation((float) elapsed
						/ duration);
				double lng = t * Constants.Zhenzhong.longitude + (1 - t)
						* startLatLng.longitude;
				double lat = t * Constants.Zhenzhong.latitude + (1 - t)
						* startLatLng.latitude;
				marker.setPosition(new LatLng(lat, lng));
				aMap.invalidate();// 刷新地图
				if (t < 1.0) {
					handler.postDelayed(this, 16);
				}
			}
		});

	}

	/**
	 * 监听点击infowindow窗口事件回调
	 */
	@Override
	public void onInfoWindowClick(Marker marker) {
		//ToastUtil.show(this, "你点击了infoWindow窗口" + marker.getTitle());			
	}

	/**
	 * 监听amap地图加载成功事件回调
	 */
	@Override
	public void onMapLoaded() {
		// 设置所有maker显示在当前可视区域地图中
		LatLngBounds bounds = new LatLngBounds.Builder()
				.include(Constants.Babaocun).include(Constants.Laojiezi).include(Constants.Zhenzhong)
				.include(Constants.Tianshengqiaocun)
				.include(Constants.Shuijingwan)
				.build();
		aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 10));
	}

	/**
	 * 监听自定义infowindow窗口的infocontents事件回调
	 */
	@Override
	public View getInfoContents(Marker marker) {
		return null;
	}

	/**
	 * 监听自定义infowindow窗口的infowindow事件回调
	 */
	@Override
	public View getInfoWindow(Marker marker) {
		return null;
	}

	/**
	 * 对单击地图事件回调
	 */
	//@Override
	public void onMapClick(LatLng point) {
	
	}

	/**
	 * 对长按地图事件回调
	 */
	//@Override
	public void onMapLongClick(LatLng point) {			
		mInputText.setVisibility(View.VISIBLE); 
		mInputText2.setVisibility(View.VISIBLE);
		ybutton.setVisibility(View.VISIBLE); 
		nbutton.setVisibility(View.VISIBLE); 	
		mlatitude=point.latitude;
		mlongitude=point.longitude;				
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button1:
			{

				mInputText.setVisibility(View.INVISIBLE); 
				mInputText2.setVisibility(View.INVISIBLE);
				ybutton.setVisibility(View.INVISIBLE); 
				nbutton.setVisibility(View.INVISIBLE);			
				mtitle=mInputText.toString();
				mcontent=mInputText2.toString();
				LatLng mpos =new LatLng(mlatitude, mlongitude);								
				aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
						.position(mpos).title(mtitle)
						.snippet(mcontent).draggable(true));	
			}
			break;
		case R.id.button2:
			 {
				mInputText.setVisibility(View.INVISIBLE); 
				mInputText2.setVisibility(View.INVISIBLE);
				ybutton.setVisibility(View.INVISIBLE); 
				nbutton.setVisibility(View.INVISIBLE); 
				mtitle="";
				mcontent="";
			}
			break;
		default:
			break;
		}
	}

}



	



